package linkwechat.secirityhandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

// 配置类 服务启动时加载
@Configuration
public class CorsConfiler {
    @Bean
    //表示该bean在ioc容器里的执行优先级 （数小优先级高）
    @Order(value = Ordered.HIGHEST_PRECEDENCE)  // 设置最小值 优先级最高
    public CorsFilter filter(){
        //跨域配置对象
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //添加允许访问的请求头：Content-Type
        corsConfiguration.addAllowedHeader("*");
        //添加允许访问的请求方法：get，post
        corsConfiguration.addAllowedMethod("*");
        //添加允许访问的源: 允许携带凭证 cookie
        corsConfiguration.addAllowedOrigin("*");
        //添加允许是否携带凭证:cookie
        corsConfiguration.addAllowedOriginPattern("*");
        //基于url的跨域配置
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        // 过滤器
        CorsFilter corsFilter = new CorsFilter(corsConfigurationSource);
        return corsFilter;
    }

}
